package home;

import includes.MD5;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import treasurer.TreasurerHome;

import classes.Account;
import classes.User;

public class HomePage extends WebPage{

	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model userModel = new Model();
	private Model passwdModel = new Model();
	MD5 md5 = new MD5 (); 
	
	public HomePage()throws IOException, SQLException {
		
		TextField id = new TextField("student_id", userModel);
		id.setRequired(true);
		
		TextField pass = new PasswordTextField("password", passwdModel);
		pass.setRequired(true);
		
		Form form = new Form ("loginForm"){
			
			private static final long serialVersionUID = 1L;
			
			protected void onSubmit() {
				try {
					String studentid = (String) userModel.getObject();
					String password = (String) passwdModel.getObject();
					String newPassword = md5.md5(password);
					
					Account account = (Account)sqlMap.queryForObject("Account.getAccount", studentid);
					
					if(account!=null && newPassword.equals(account.getPassword()) ){
						
						if((Integer.toString(account.getUser_id())).equals("1")){						
							
							//set session to current user
							((Sessions)getSession()).setUser_id(account.getUser_id());
							((Sessions)getSession()).setStudent_id(studentid);
							
							//search for user description 
							User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUser_id() );
							
							//set current user description
							((Sessions)getSession()).setDescription(user.getUserdesc(account.getUser_id()));	
							
							TreasurerHome page = new TreasurerHome();
							setResponsePage(page);
						}
						
						if((Integer.toString(account.getUser_id())).equals("1")){						
							
							//set session to current user
							((Sessions)getSession()).setUser_id(account.getUser_id());
							((Sessions)getSession()).setStudent_id(studentid);
							
							//search for user description 
							User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUser_id() );
							
							//set current user description
							((Sessions)getSession()).setDescription(user.getUserdesc(account.getUser_id()));	
							
							TreasurerHome page = new TreasurerHome();
							setResponsePage(page);
						}
						
						
					}
					else {
						error ("Invalid Username and Password!");
					}
				}catch (SQLException e){
					error("Invalid Username and Password. Try again.");
					e.printStackTrace();
					
				}
			}
		};
		
		add (new FeedbackPanel("error"));
		add(new BookmarkablePageLink("home", HomePage.class));
		
		form.add(id);
		form.add(pass);	
		add(form);
	}
}
